package com.zx.map.controller;


import com.zx.map.dao.BalanceSummaryDao;
import com.zx.map.dao.OrdLogDao;
import com.zx.map.dao.model.BalanceSummary;
import com.zx.map.dao.model.BalanceSummaryExample;
import com.zx.map.service.AgentInfoService;
import com.zx.map.service.OrdLogService;
import com.zx.map.service.TodoTaskService;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.MainPageModel;
import com.zx.platform.service.domain.LoginDomain;
import com.zx.platform.utils.PmpUtils;
import com.zx.platform.utils.StrUtils;
import com.zx.utils.WebUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 首页功能
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/manage/mainPage")
public class MainPageController {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(MainPageController.class);

	@Autowired
	private TodoTaskService todoTaskService;
	@Autowired  
	private OrdLogService ordLogService;
	@Autowired  
	private OrdLogDao ordLogDao;
	@Autowired
	private AgentInfoService agentInfoService;
	@Autowired
	private BalanceSummaryDao balanceSummaryDao;
	
	/**
     * @return
     */
    @RequestMapping("/getData")
    public
    @ResponseBody
    MainPageModel getData(MainPageModel model) {
        try {
        	//商户申请待审核
        	Integer merchantInfoToAudit = todoTaskService.getCountBytaskType("1", "0");
        	//终端绑定待审核
        	Integer terminalBandingToAudit = todoTaskService.getCountBytaskType("2", "0");
        	//终端解绑待审核
        	Integer terminalUnBandingToAudit = todoTaskService.getCountBytaskType("3", "0");
        	//调账待复核
        	Integer adjustToAudit = todoTaskService.getCountBytaskType("7", "0");
        	//结算待复核-单
        	Integer singleSettleToAudit = todoTaskService.getCountBytaskType("4", "0");
        	//结算待复核-批
        	Integer batchSettleToAudit = todoTaskService.getCountBytaskType("5", "0");
        	//结算银行待复核
        	Integer bankChangeToAudit = todoTaskService.getCountBytaskType("6", "0");
			//线下收单审核
			Integer offlineToAudit = todoTaskService.getCountBytaskType("8", "0");
        	//收款笔数
        	Integer  gatheringCount=ordLogDao.geGatheringCount();
        	//收款金额        	
        	Double gatheringAmt= StrUtils.get2Double(ordLogDao.geGatheringAmt());
        	//结算金额
        	Double settleAmt=StrUtils.get2Double(ordLogDao.geSettleAmt());
        	//余额
        	Double backAccountAmt=StrUtils.get2Double(ordLogDao.geBackAccountAmt());
        	//TO额度
        	Double t0Amt=StrUtils.get2Double(ordLogDao.geT0Amt());
        	//结算笔数
        	Integer settleCout=ordLogDao.geSettleCount();

	        BalanceSummaryExample example = new BalanceSummaryExample();
	        BalanceSummaryExample.Criteria criteria = example.createCriteria();
	        String today = PmpUtils.dateToString(new Date(), "yyyy-MM-dd");
	        criteria.andCreateDateEqualTo(today);
	        List<BalanceSummary> list = balanceSummaryDao.getListByExample(example);
	        BalanceSummary balanceSummary = list.get(0);

	        //账户总余额
	        double accoutTotalAvlBal = balanceSummary.getAccountBal();
	        //账户总冻结余额
	        double accoutTotalFrzBal = balanceSummary.getAccountFrzBal();
	        //分享收益总可用余额
	        double shareProfitTotalAvlBal = balanceSummary.getShareAvlBal();
	        //分享收益总余额
	        double shareProfitTotalBal = balanceSummary.getShareBal();
	        //昨日总分润金额
	        double yesterdayTotalShareProfit = balanceSummary.getYesterdayShare();

	        /*//账户总余额
	        double accoutTotalAvlBal = SaveHistoryDataTimer.accoutTotalAvlBal;
	        //账户总冻结余额
	        double accoutTotalFrzBal = SaveHistoryDataTimer.accoutTotalFrzBal;
	        //分享收益总可用余额
	        double shareProfitTotalAvlBal = SaveHistoryDataTimer.shareProfitTotalAvlBal;
	        //分享收益总余额
	        double shareProfitTotalBal = SaveHistoryDataTimer.shareProfitTotalBal;
	        //昨日总分润金额
	        double yesterdayTotalShareProfit = SaveHistoryDataTimer.yesterdayTotalShareProfit;*/

        	model.setSettleCount(settleCout);
        	model.setGatheringAmt(gatheringAmt);
        	model.setBackAccountAmt(backAccountAmt);
        	model.setT0Amt(t0Amt);
        	model.setSettleAmt(settleAmt);
        	model.setGatheringCount(gatheringCount);
        	model.setMerchantInfoToAudit(merchantInfoToAudit);
        	model.setTerminalBandingToAudit(terminalBandingToAudit);
        	model.setTerminalUnBandingToAudit(terminalUnBandingToAudit);
        	model.setAdjustToAudit(adjustToAudit);
        	model.setSingleSettleToAudit(singleSettleToAudit);
        	model.setBatchSettleToAudit(batchSettleToAudit);
        	model.setBankChangeToAudit(bankChangeToAudit);
			model.setOfflineToAudit(offlineToAudit);
			model.setAccoutTotalAvlBal(accoutTotalAvlBal);
			model.setAccoutTotalFrzBal(accoutTotalFrzBal);
			model.setShareProfitTotalAvlBal(shareProfitTotalAvlBal);
			model.setShareProfitTotalBal(shareProfitTotalBal);
	        model.setYesterdayTotalShareProfit(yesterdayTotalShareProfit);
        	model.setSuccess(true);
        	model.setMsg("获取信息成功");
            log.debug("获取信息成功");
        } catch (Exception e) {
        	e.printStackTrace();
        	model.setSuccess(false);
        	model.setMsg("获取信息失败");
            log.error("获取信息失败" + e.getMessage(), e);
        }
        return model;
    }

    /**
     * @return
     */
    @RequestMapping("/getAgentData")
    @ResponseBody
    public MainPageModel getAgentData(MainPageModel model,HttpServletRequest request) {
        try {
        	// 查找当前操作员对应的代理商
			AgentInfoModel curAgentInfoModel = WebUtils.queryCurrentLoginAgentInfo(request);
	        curAgentInfoModel.setHasSelf(true);
			List values = agentInfoService.queryDownAgentIdList(curAgentInfoModel);
			if(values==null||values.size()==0){
				values = new ArrayList();
				values.add("-1");
	       	}
        	//收款笔数
        	Integer  gatheringCount=ordLogDao.geGatheringCountByAgent(values);
        	//收款金额        	
        	Double gatheringAmt= StrUtils.get2Double(ordLogDao.geGatheringAmtByAgent(values));
        	
        	model.setGatheringAmt(gatheringAmt);
        	model.setGatheringCount(gatheringCount);
        	
        	model.setSuccess(true);
        	model.setMsg("获取信息成功");
            log.debug("获取信息成功");
        } catch (Exception e) {
        	e.printStackTrace();
        	model.setSuccess(false);
        	model.setMsg("获取信息失败");
            log.error("获取信息失败" + e.getMessage(), e);
        }
        return model;
    }

    @RequestMapping("/main")
	public String main(LoginDomain loginDomain, Model model,
			HttpServletRequest request) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
    	try{
			return "jsp/platform/main";
		} catch (Exception e) {
			model.addAttribute("errorMsg", "系统内部异常");
			log.info("系统内部异常", e);
			return "/login";
		}
	}

    @RequestMapping("/agentDesk")
	public String agentDesk(LoginDomain loginDomain, Model model,
			HttpServletRequest request) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
    	try{
			return "jsp/platform/agentDesk";
		} catch (Exception e) {
			model.addAttribute("errorMsg", "系统内部异常");
			log.info("系统内部异常", e);
			return "/login";
		}
	}
}
